package com.bsoft.gol.hbs.message.sendMsg;

import com.bsoft.gol.hbs.utils.ResponseCode;
import hcn.bean.SendMsgDTO;
import hcn.util.BaseResponse;
import hcn.service.base.sendMsg.ISendMsgService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

/**
 * @description:抽象的发送消息实现
 * @author: zhengcb10708
 * @create: 2020-09-24 18:25
 **/
public abstract class AbstractSendMsgServiceImpl implements ISendMsgService {


    private final static Logger logger = LoggerFactory.getLogger(AbstractSendMsgServiceImpl.class);

   /**
    * @Describe: 发送消息
    * @Param: [sendMsgDTO, content]
    * @Return: BaseResponse
    * @Author: zhengcb10708
    * @Date: 2020/9/24 18:36
    */
    @Override
    public BaseResponse sendMsg(SendMsgDTO sendMsgDTO, String content) {
        logger.info(getName() + ": 发送消息入参:" + sendMsgDTO);
        BaseResponse baseResponse = new BaseResponse();
        if (isDoSendMsg()) {
            baseResponse = doSendMsg(sendMsgDTO, content);
        } else {
            baseResponse.setCode(ResponseCode.FAIL);
            baseResponse.setMessage(getName() + "发送消息的发送条件不满足，本条发送的消息被略过。");
        }
        logger.info("{}: 发送消息出参:{}", getName(), baseResponse);
        return baseResponse;
    }

    @Override
    public BaseResponse sendPushMsg(SendMsgDTO sendMsgDTO, String content) {
        logger.info(getName() + ": 发送消息入参:" + sendMsgDTO);
        BaseResponse baseResponse = new BaseResponse();
        if (isDoSendMsg()) {
            baseResponse = doSendPushMsg(sendMsgDTO, content);
        } else {
            baseResponse.setCode(ResponseCode.FAIL);
            baseResponse.setMessage(getName() + "发送消息的发送条件不满足，本条发送的消息被略过。");
        }
        logger.info("{}: 发送消息出参:{}", getName(), baseResponse);
        return baseResponse;
    }

    /**
     * @Describe: 根据美元符号删除内容
     * @Param: [content]
     * @Return: java.lang.String
     * @Author: zhengcb10708
     * @Date: 2020/9/24 18:36
     */
    public String deleteStringByDollarSign(String content) {
        if (content == null) {
            return null;
        }
        StringBuilder stringBuilder = new StringBuilder(content);
        int topDollarSignIndex = -1;
        while (stringBuilder.indexOf("$", topDollarSignIndex + 1) > -1) {
            if (topDollarSignIndex == -1) {
                topDollarSignIndex = stringBuilder.indexOf("$");
            } else {
                stringBuilder.delete(topDollarSignIndex, stringBuilder.indexOf("$", topDollarSignIndex + 1) + 1);
                topDollarSignIndex = -1;
            }
        }
        return stringBuilder.toString();
    }

  /**
   * @Describe: 判断是否进行发消息操作-主要进行配置参数的校验
   * @Param: []
   * @Return: boolean
   * @Author: zhengcb10708
   * @Date: 2020/9/24 18:36
   */
    protected abstract boolean isDoSendMsg();

    /**
     * @Describe: 发送消息具体实现
     * @Param: [sendMsgDTO, content]
     * @Return: BaseResponse
     * @Author: zhengcb10708
     * @Date: 2020/9/24 18:37
     */
    protected abstract BaseResponse doSendMsg(SendMsgDTO sendMsgDTO, String content);
    /**
     * @Describe: 发送手工推送消息具体实现
     * @Param: [sendMsgDTO, content]
     * @Return: BaseResponse
     * @Author: zhengcb10708
     * @Date: 2021/9/8 10:49
     */
    protected abstract BaseResponse doSendPushMsg(SendMsgDTO sendMsgDTO, String content);


    /**
    * @Describe: 得到具体实现类的名称
    * @Param: []
    * @Return: java.lang.String
    * @Author: zhengcb10708
    * @Date: 2020/9/24 18:37
    */
    protected abstract String getName();

}
